package com.ruoyi.web.controller.shop;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.shop.domain.ShopPointsOrders;
import com.ruoyi.shop.domain.ShopProducts;
import com.ruoyi.shop.service.IShopPointsOrdersService;
import com.ruoyi.shop.service.IShopProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 积分兑换订单Controller
 * 
 * @author liuwp
 * @date 2025-11-24
 */
@RestController
@RequestMapping("/shop/shop_points_orders")
public class ShopPointsOrdersController extends BaseController
{
    @Autowired
    private IShopPointsOrdersService shopPointsOrdersService;
    @Autowired
    private IShopProductsService shopProductsService;

    /**
     * 查询积分兑换订单列表
     */
    @PreAuthorize("@ss.hasPermi('shop:shop_points_orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShopPointsOrders shopPointsOrders)
    {
        startPage();
        List<ShopPointsOrders> list = shopPointsOrdersService.selectShopPointsOrdersList(shopPointsOrders);
        return getDataTable(list);
    }

    /**
     * 导出积分兑换订单列表
     */
    @PreAuthorize("@ss.hasPermi('shop:shop_points_orders:export')")
    @Log(title = "积分兑换订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopPointsOrders shopPointsOrders)
    {
        List<ShopPointsOrders> list = shopPointsOrdersService.selectShopPointsOrdersList(shopPointsOrders);
        ExcelUtil<ShopPointsOrders> util = new ExcelUtil<ShopPointsOrders>(ShopPointsOrders.class);
        util.exportExcel(response, list, "积分兑换订单数据");
    }

    /**
     * 获取积分兑换订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        ShopPointsOrders shopPointsOrders = shopPointsOrdersService.selectShopPointsOrdersById(id);
        shopPointsOrders.setShopProducts(shopProductsService.selectShopProductsById(shopPointsOrders.getProductsId()));
        return AjaxResult.success(shopPointsOrders);
    }

    /**
     * 新增积分兑换订单
     */
    @PreAuthorize("@ss.hasPermi('shop:shop_points_orders:add')")
    @Log(title = "积分兑换订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopPointsOrders shopPointsOrders)
    {
        return toAjax(shopPointsOrdersService.insertShopPointsOrders(shopPointsOrders));
    }

    /**
     * 修改积分兑换订单
     */
    @PreAuthorize("@ss.hasPermi('shop:shop_points_orders:edit')")
    @Log(title = "积分兑换订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopPointsOrders shopPointsOrders)
    {
        return toAjax(shopPointsOrdersService.updateShopPointsOrders(shopPointsOrders));
    }

    /**
     * 删除积分兑换订单
     */
    @PreAuthorize("@ss.hasPermi('shop:shop_points_orders:remove')")
    @Log(title = "积分兑换订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shopPointsOrdersService.deleteShopPointsOrdersByIds(ids));
    }

    @GetMapping("/query")
    public TableDataInfo query(ShopPointsOrders shopPointsOrders)
    {
        shopPointsOrders.setUserId(SecurityUtils.getUserId());
        startPage();
        List<ShopPointsOrders> list = shopPointsOrdersService.selectShopPointsOrdersList(shopPointsOrders);
        list.forEach(item -> {
            item.setShopProducts(shopProductsService.selectShopProductsById(item.getProductsId()));
        });
        return getDataTable(list);
    }


    @Log(title = "积分兑换下单", businessType = BusinessType.INSERT)
    @PostMapping("/exchange")
    public AjaxResult exchange(@RequestBody ShopPointsOrders shopPointsOrders)
    {
        // 传进来的是产品id，地址id 兑换数量
        if (shopPointsOrders.getProductsId() == null || shopPointsOrders.getAddressId() == null || shopPointsOrders.getProductCount() == null)
        {
            return AjaxResult.error(MessageUtils.message("ora.common.required"));
        }

        // 获取产品
        ShopProducts shopProducts = shopProductsService.selectShopProductsById(shopPointsOrders.getProductsId());
        if (shopProducts == null)
        {
            return AjaxResult.error(MessageUtils.message("ora.common.required"));
        }

        shopPointsOrders.setUserId(SecurityUtils.getUserId());

        return shopPointsOrdersService.exchangeShopPointsOrders(shopPointsOrders, shopProducts);
    }

}
